package com.inews.business.service;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.inews.base.exception.BusinessException;
import com.inews.base.mapper.BeanMapper;
import com.inews.base.persistence.DynamicSpecifications;
import com.inews.base.persistence.SearchFilter;
import com.inews.base.persistence.SearchFilter.Operator;
import com.inews.base.security.vo.BaseUserVo;
import com.inews.base.utils.ExcelUtil;
import com.inews.base.utils.ExtDataUtil;
import com.inews.base.utils.InewsUtils;
import com.inews.base.vo.ExtData;
import com.inews.business.constants.InewsBusinessConstants;
import com.inews.business.entity.TblSalaryChange;
import com.inews.business.entity.TblTeacher;
import com.inews.business.entity.TblTrainInfo;
import com.inews.business.entity.TblSysUser;
import com.inews.business.repository.ITrainInfoDao;
import com.inews.business.vo.DegreeStudyVo;
import com.inews.business.vo.SalaryChangeVo;
import com.inews.business.vo.TeacherVo;
import com.inews.business.vo.TrainInfoVo;

@Service
public class TrainInfoServiceImpl implements ITrainInfoService {
	@Autowired
	private ITrainInfoDao trainDao;
	
	@Autowired
	private BaseService baseService;
	
	@Autowired
	private ITeacherService teacherService;
	
	@Override
	public ExtData<TrainInfoVo> findTrainPage(
			Map<String, Object> filterParams, Pageable page) {
		List<TrainInfoVo> voList = new ArrayList<TrainInfoVo>();
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		filters.put("status", new SearchFilter("status", Operator.EQ, InewsBusinessConstants.TRAIN_STATUS_NORMAL));
		Page<TblTrainInfo> pagePoList = trainDao.findAll(DynamicSpecifications.bySearchFilter(filters.values(), TblTrainInfo.class), page);
		for (TblTrainInfo po : pagePoList.getContent()) {
			TrainInfoVo vo = new TrainInfoVo();
			//添加人员信息
			teacherService.setTeacherInfo(vo, po.getStaffId());
			BeanMapper.copy(po, vo);
			
//			vo.setRetireDateFormat(InewsUtils.fomatDate(po.getRetireDate()));
//			vo.setCountryPayFormat(InewsUtils.formatCurrency2String(po.getCountryPay()));
//			vo.setCountryPayAddFormat(InewsUtils.formatCurrency2String(po.getCountryPayAdd()));
//			vo.setAllowanceFormat(InewsUtils.formatCurrency2String(po.getAllowance()));
			//Long basePay = po.getCountryPay() + po.getCountryPayAdd();
			//vo.setBasePayFormat(InewsUtils.formatCurrency2String(basePay));
			//Long sumPay = po.getCountryPay() + po.getCountryPayAdd() + po.getAllowance();
			//vo.setSumPayFormat(InewsUtils.formatCurrency2String(sumPay));
			voList.add(vo);
		}
		ExtData<TrainInfoVo> extData = ExtDataUtil.genWithPagingList(voList, pagePoList.getTotalElements());
		return extData;
	}

	@Override
	@Transactional(readOnly=false)
	public ExtData<TrainInfoVo> saveTrain(TrainInfoVo vo, BaseUserVo user) {
		String currentTime = baseService.getCurrentTime();
		TblSysUser editUser = baseService.getEditUser(user);
		TblTrainInfo po = BeanMapper.map(vo, TblTrainInfo.class);
		po.setStatus(InewsBusinessConstants.TRAIN_STATUS_NORMAL);		
		po.setCreateTime(currentTime);
		po.setCreateUser(editUser);
		po.setLastUpdUser(editUser);
		po.setLastUpdDes(InewsBusinessConstants.LAST_UPD_DES_ADD);
     	po.setLastUpdTime(currentTime);
		trainDao.save(po);
		return ExtDataUtil.genWithSingleData(vo);
	}

	@Override
	@Transactional(readOnly=false)
	public ExtData<TrainInfoVo> updateTrain(TrainInfoVo vo,
			BaseUserVo user) {
		String currentTime = baseService.getCurrentTime();
		TblSysUser editUser = baseService.getEditUser(user);
		TblTrainInfo po = trainDao.findOne(vo.getSn());	
		BeanMapper.copy(vo,po);		
		po.setLastUpdUser(editUser);
		po.setStatus(InewsBusinessConstants.TRAIN_STATUS_NORMAL);		

		po.setLastUpdDes(InewsBusinessConstants.LAST_UPD_DES_UPDATE);
		po.setLastUpdTime(currentTime);
		trainDao.save(po);
		return ExtDataUtil.genWithSingleData(vo);
	}

	@Override
	@Transactional(readOnly=false)
	public ExtData<TrainInfoVo> deleteTrain(TrainInfoVo vo,
			BaseUserVo user) {
		String currentTime = baseService.getCurrentTime();
		TblSysUser editUser = baseService.getEditUser(user);
		TblTrainInfo po = trainDao.findOne(vo.getSn());	
		po.setStatus(InewsBusinessConstants.TRAIN_STATUS_DELETE);
		po.setLastUpdUser(editUser);
		po.setLastUpdDes(InewsBusinessConstants.LAST_UPD_DES_DELETE);
		po.setLastUpdTime(currentTime);
		trainDao.save(po);
		return ExtDataUtil.genWithSingleData(vo);
	}
	
@Override
	/**
	 * 导出excel.
	 *
	 * @param filterParams the filter params
	 * @param page the page
	 * @return the ext data
	 */
	public ExtData<TrainInfoVo> exportExcel(Map<String, Object> filterParams){
		List<TrainInfoVo> allDegreeStudyVoList = new ArrayList<TrainInfoVo>();
		
		
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		
		filters.put("status", new SearchFilter("status", Operator.EQ, InewsBusinessConstants.TRAIN_STATUS_NORMAL));
		List<TblTrainInfo> result=	trainDao.findAll(DynamicSpecifications.bySearchFilter(filters.values(), TblTrainInfo.class));
				for (TblTrainInfo po : result) {
					TrainInfoVo svo = BeanMapper.map(po, TrainInfoVo.class);

					allDegreeStudyVoList.add(svo);
				}
				
		
		return ExtDataUtil.genWithData(allDegreeStudyVoList);
	}
	
@Override
/**
 * 导入excel.
 *
 * @param excelFile the excel file
 * @param user the user
 * @return the ext data
 */
@Transactional(readOnly=false)
public ExtData<String> importExcel(InputStream is, BaseUserVo user){
	
	try {
		List<Object> voList =ExcelUtil.ImportVo(is, ExcelUtil.TrainValue, TrainInfoVo.class);
		 for (Object obj : voList) {
			 TrainInfoVo vo=(TrainInfoVo)obj;
		    	if(vo.getSn()==null||trainDao.exists(vo.getSn())){
		    		updateTrain(vo, user);
				}else{
					saveTrain(vo, user);
				}
			}
			return ExtDataUtil.genWithSingleData("导入成功");
	} catch (Exception e) {		   
		throw new BusinessException("导入失败 失败的原因是"+e.getMessage());
	}
}
}
